<?php
namespace Modules\ApiV2\Controllers;

use App\Models\OilCard;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Modules\Service\Logic\OilCardLogic;
use Modules\Service\Logic\OilcardOrderLogic;
use Modules\Token\Facades\Token;

class OilCardController extends BaseController{

    //绑定油卡
    public function bindOilCard(Request $request){
        try {
            //获取用户Id
            $userId = Token::data("id");
            //获取参数
            $card_no = $request->input("card_no", false);
            $type = $request->input("type", false);
            $name = $request->input("name", false);
            $mobile = $request->input("mobile",false);
            //检测参数是否合法
            if (!$card_no) return self::fail(1003, "缺少必要参数:card_no");
            if (!$type) return self::fail(1003, "缺少必要参数:type");
            if (!$name) return self::fail(1003, "缺少必要参数:name");
            if (!$mobile) return self::fail(1003, "缺少必要参数:mobile");

            if (!preg_match('/^1\d{18}$/',$card_no,$arr) && !preg_match('/^9\d{15}$/',$card_no,$arr)){
                return self::fail(1036,"亲!油卡格式不正确哟!");
            }
            //判断油卡绑定人数
            $count = OilCard::where("card_no",$card_no)->where("status",1)->count();
            if ($count>=2)return self::fail(1041,"油卡绑定已达上限,请更换绑定油卡");
            //判断用户是否重复绑定
            $userOilCard =  OilCard::where("card_no",$card_no)->where("user_id",$userId)->first();
            if ($userOilCard){
                if ($userOilCard->status==1) {
                    return self::fail(1023, "请不要重复绑定油卡");
                }else{
                    $userOilCard->status = 1;
                    $userOilCard->mobile = $mobile;
                    $userOilCard->type = $type;
                    $userOilCard->name = $name;
                    $userOilCard->save();
                    return success();
                }
            }

            //判断油卡号属于哪家集团
            if (preg_match('/^(90*)/',$card_no,$arr)){
                $type = 2;
            }else{
                $type = 1;
            }
            return OilCardLogic::bindOilCard($userId,$card_no,$type,$name,$mobile);
        }catch (\Exception $e){
            Log::error($e);
            return self::fail(500,"系统异常");
        }
    }

    // 计算运费
    public function countExpress(Request $request){
        //获取参数
        $rechargeId = $request->input("recharge_id",0);
        $amount = $request->input("amount",0);
        return OilcardOrderLogic::oilOrderExpress($rechargeId,$amount);
    }

    //领取油卡
    public function newUserGetOilCard(Request $request){
        //获取用户Id
        $userId = Token::data("id");
        //获取参数
        $rechargeId = $request->input("recharge_id",false);//套餐ID
        $type = $request->input("type");//领取油卡
        $addressId = $request->input("address_id",false);//收货地址ID
        $couponId = $request->input("coupon_id",false);//优惠券ID
        $payType = $request->input("pay_type", 'weixin');//支付方式
        $amount = $request->input("amount", false);//充值金额
        $openid = $request->input("openid",false);//微信支付openid
        if (!$rechargeId)return self::fail(1003,"缺少必要参数:recharge_id");
        if (!$addressId)return self::fail(1003,"缺少必要参数:address_id");
        if (!$amount)return self::fail(1003,"缺少必要参数:amount");
        if (!$type)return self::fail(1003,"缺少必要参数:type");
        if (!$payType || ($payType!='weixin' && $payType!='alipay' && $payType!='weixin_app'))return self::fail(1003,"缺少必要参数:pay_type");
        return OilcardOrderLogic::getOilCardByNewUser($userId,$addressId,$rechargeId,$couponId,$payType,$amount,$openid,$type);
    }
}